﻿// ———————————————————————–
// <copyright file="RouterRuleService.svc.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// RouterRuleService.svc.cs - "Allows Router Rule Service To Interact With The DAL"
// Project: "IC.NETSOA"- "ICNETREST"
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
                
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Xml.Linq;
using ICNETDAL;
using ICNETSerializers;

namespace ICNETREST
{
  /// <summary>
  /// Contains methods that the RouterRules services uses to modify, delete, of get rules from the database.
  /// </summary>
  [ServiceContract(Namespace = "urn:icnet:mitre:org:RouterRuleService")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  public class RouterRuleService
  {
    /// <summary>
    /// Gets the router rules.
    /// </summary>
    /// <returns>The router rules, formatted as an XML object.</returns>
    [WebGet(UriTemplate = "/", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetRouterRules()
    {
      return AWSRouterRulesDAL.GetRouterRulesXML("","");
    }

    /// <summary>
    /// Gets the rule associated with a certain ID.
    /// </summary>
    /// <param name="ValueListURN">ValueList urn used in looking up the rule.</param>
    /// <param name="Value">Value urn used in looking up the rule.</param>
    /// <returns>Thoe role associated with the ID, formatted as an XML object.</returns>
    [WebGet(UriTemplate = UriTemplates.RuleUri, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetRouterRule(string ValueListURN, string Value)
    {
      return AWSRouterRulesDAL.GetRouterRulesXML(ValueListURN, Value);
    }

    /// <summary>
    /// Creates a new router rule and adds it to the database.
    /// </summary>
    /// <param name="xe">An XML object that will be used to create the rule in XML format.</param>
    /// <returns>The router rule that was added to the database. It will be be noted inside the XML object if any fields were left blank.</returns>
    [WebInvoke(UriTemplate = "/", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement CreateNewRule(XElement xe)
    {
      return AWSRouterRulesDAL.AddRouterRules(RouterRulesHelper.FromXElement(xe)); 
    }  

    /// <summary>
    /// Deletes a rule from the database. 
    /// </summary>
    /// <param name="Id">The ID of the role to delete.</param>
    /// <returns>The object that was deleted, as an XML object.</returns>
    [WebInvoke(UriTemplate = UriTemplates.RuleUri, Method = "DELETE", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement DeleteRule(string ValueListURN, string Value)
    {
      return AWSRouterRulesDAL.DeleteRouterRules(ValueListURN, Value);
    }

    /// <summary>
    /// Creates a new router rule and adds it to the database.
    /// </summary>
    /// <param name="xe">An XML object that will be used to create the rule in XML format.</param>
    /// <returns>The router rule that was added to the database. It will be be noted inside the XML object if any fields were left blank.</returns>
    [WebInvoke(UriTemplate = "/UpdateRule", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement UpdatePartsOfRule(XElement xe)
    {
      return AWSRouterRulesDAL.UpdateRouterRule(false, RouterRulesHelper.FromXElement(xe));
    }

    /// <summary>
    /// Deletes a rule from the database. 
    /// </summary>
    /// <param name="Id">The ID of the role to delete.</param>
    /// <returns>The object that was deleted, as an XML object.</returns>
    [WebInvoke(UriTemplate = "/UpdateRule", Method = "DELETE", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement RemovePartsOfRule(XElement xe)
    {
      return AWSRouterRulesDAL.UpdateRouterRule(true, RouterRulesHelper.FromXElement(xe));
    }
  }   
}
